import { createRouter, createWebHistory } from 'vue-router';
import type { Router, RouteRecordRaw } from 'vue-router';
// 引入测试路由
import TestRoutes from './modules/test';
import { getEnv } from '@/utils';
import nprogress from 'nprogress';
import 'nprogress/nprogress.css';

const { DEV, MODE } = getEnv();

let router: Router;

export const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/e-lab',
  },
  {
    path: '/e-lab',
    component: () => import('@/views/ELN/index.vue'),
    name: 'ELNView',
  },
  {
    path: '/workflow',
    component: () => import('@/views/WorkFlow/index.vue'),
    name: 'WorkFlowView',
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404',
  },
];

if (DEV || MODE === 'development') {
  router = createRouter({
    history: createWebHistory(),
    routes: routes.concat(TestRoutes),
  });
} else {
  router = createRouter({
    history: createWebHistory(),
    routes: routes,
  });
}
router.beforeEach((to, _, next) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }

  // 开启进度条
  nprogress.start();
  next();
});

router.afterEach((to, from) => {
  // 结束进度条
  nprogress.done();
});

/**
 * 重置路由
 */
export const resetRouter = () => {
  router.replace({
    path: '/login',
  });
};

export default router;
